package com.chenfan.magic.tags;

import com.chenfan.magic.utils.IDKit;
import com.chenfan.magic.utils.StringKit;
import lombok.Data;
import org.apache.commons.lang3.StringEscapeUtils;
import org.jsoup.nodes.Element;

import java.io.Serializable;

/**
 * @author lm
 * validate标签模型
 * <validate id="123456"></validate>
 */
@Data
public class TagValidate extends TagAbstract implements Serializable {

    private static final long serialVersionUID = 1L;

    public static String tagNames = "validate";

    public TagValidate() {
    }

    public TagValidate(Element validateTag, int index) {
        this.element = validateTag.toString();
        this.id = validateTag.attr("id");
        if (StringKit.isBlank(this.id)) {
            this.id = IDKit.fastUUID() + "";
        }
        this.name = validateTag.attr("name");
        String sort = validateTag.attr("sort");
        if (StringKit.isBlank(sort)) {
            this.sort = index - 10000;
        } else {
            this.sort = Integer.parseInt(sort);
        }
        this.resultKey = validateTag.attr("result-key");
        if (StringKit.isBlank(this.resultKey)) {
            this.resultKey = "result_" + this.id;
        }
        String respond = validateTag.attr("respond");
        if (StringKit.isBlank(respond)) {
            this.respond = false;
        } else {
            this.respond = Boolean.valueOf(respond);
        }
        String resolve = validateTag.attr("resolve");
        if (StringKit.isBlank(resolve)) {
            this.resolve = true;
        } else {
            this.resolve = Boolean.valueOf(resolve);
        }
        this.context = StringEscapeUtils.unescapeHtml4(validateTag.text());

        this.__sort = validateTag.attr(":sort");
        this.__respond = validateTag.attr(":respond");
        this.__resolve = validateTag.attr(":resolve");
        this.__resultKey = validateTag.attr(":result-key");

        this.engine = validateTag.attr("engine");

    }
}
